//
//  UATRequestConfig.m
//  Pods-UStarter_Example
//
//  Created by Mark on 2021/1/7.
//

#import "UATRequestConfig.h"


@interface UATRequestConfig()

@end

@implementation UATRequestConfig


- (instancetype) initWithGlobalParam:(NSDictionary*)param scope:(UATRequestConfigScope)scope baseUrl:(NSString*)baseUrl;{
    self = [super init];
    if(self){
        self.globalParam = param;
        self.scope = scope;
        self.baseUrl = baseUrl;
    }
    return self;
}



- (UATRequestOption*)toOptWithAction:(NSString*)action param:(nullable NSDictionary*)param data:(nullable NSDictionary*)data{
    UATRequestOption * opt = [[UATRequestOption alloc] init];
    
    UATRequestConfigScope scope = self.scope ;
    NSString * url = [UatUtil combineHost:self.baseUrl action:action];
    
    
    switch (scope) {
        case UATRequestConfigScopeHEADER :
        {
            [opt setUrl:[UatUtil componentUrl:url param:param]];
            [opt setData:data];
            [opt setHeader:self.globalParam];
        }
            break;
        case UATRequestConfigScopeBODY :
        {
            [opt setUrl:[UatUtil componentUrl:url param:param]];
            NSMutableDictionary * p = [NSMutableDictionary dictionary];
            [p addEntriesFromDictionary:self.globalParam];
            [p addEntriesFromDictionary:data];
            [opt setData:p];
            [opt setHeader:@{}];
        }
            break;
        case UATRequestConfigScopeURL:
        default:
        {
            NSMutableDictionary * p = [NSMutableDictionary dictionary];
            [p addEntriesFromDictionary:self.globalParam];
            [p addEntriesFromDictionary:param];
            [opt setUrl:[UatUtil componentUrl:url param:p]];
            [opt setData:data==nil?@{}:data];
            [opt setHeader:@{}];
            
        }
            break;;
    }
    DLog(@"request->%@",opt);
    return opt;
}


@end
